const { Tab, extend } = require('../../dist/index');
var app=getApp();
Page(extend({}, Tab, {
  data: {
    inputShowed: false,
    inputVal: "",
    couponList: [],
    inputContent: "",
    selectCategory: "",
    sorts: "hot",
    types:"defalut",
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    cate_method: {},    
    sort_method: {},
    cate_src: [],
    picWidth: wx.getSystemInfoSync().windowWidth,
    pageIndex:1,
    menu_cate:[{name:"综合",id:"0"}],
    imgUrls: [
      {"image":'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg'},   
    ],
  },
  onShareAppMessage: function () {

  },
  onShow: function () {
    this.setData({
      isLoading: true,
      loadOver: false,
    });
    if (this.data.theconfig.wxname != undefined){
    wx.setNavigationBarTitle({
      title: this.data.theconfig.wxname
    });
  }
  },
  onReady:function(){
    this.setData({ is_kouling: app.globalData.is_kouling})

  },

  onLoad: function (options) {
    // this.getCategoryList()
    this.getcate();
    this.getMoreCouponList();
    this.getbanner();
    var that = this;
      this.setData({
        theconfig: wx.getStorageSync('theconfig'),
        sort_method: {
          list: [{
            id: 'hot',
            name: '销量'
          },{
            id: 'new',
            name: '最新'
          },{
              id: 'low',
            name: '券额'
          },  {
              id: 'price',
            name: '价格'
          }],
          selectedId: 'hot'
        },
               
      })
    
  },
  //取分类
  getcate:function(){
    var that=this
    wx.request({
      url: app.globalData.apiurl,
      data: { a:"cate_list"},
      success:function(res){
       // console.log(res.data.data.result.cate_list)
        that.setData({
          cate_method: {
            list: that.data.menu_cate.concat(res.data.data.result.cate_list),
            selectedId: "0",
            scroll: true,
            height: 42,
            }
        })
        wx.setStorageSync("menu_cate", that.data.cate_method)
      }
    })
  },//取banner
  getbanner: function () {
    var that = this
    wx.request({
      url: app.globalData.httpsurl,
      data: {
        action: "getbanner",
        userid: (wx.getStorageSync('theconfig')).userid,

      },
      method: "GET",
      success: function (res) {
        //console.log(res.data.data)
        if (res.data.data != null && res.data.data.length > 0){
        that.setData({
          imgUrls: res.data.data
        })
        } 
      }


    })
  },
  get99:function(){
    wx.switchTab({
      url: "/pages/99/99",
    })
  },
  get20: function () {
   wx.navigateTo({
     url: '/pages/20/20',
   })
  },
  getdeq: function () {
    wx.navigateTo({
      url: '/pages/large/large',
    })
  },
  getjxquan: function () {
    wx.navigateTo({
      url: '/pages/jxquan/jxquan',
    })
  },
  //取商品
  getMoreCouponList: function () {
    var that = this
    wx.request({
      url: app.globalData.apiurl,
       data: {
         "a":"itemlist",   
         "type": that.data.types, 
         "sort":that.data.sorts,     
         "p": that.data.pageIndex,
         "num":"5",
         "isq":2,
         "cid": that.data.selectCategory

       },
       method: "GET",
       success: function (resRequest) {
        // console.log(resRequest.data.data.result);
         if (resRequest.statusCode === 200) {
           if (resRequest.data.data.result != null && resRequest.data.data.result.length >0) {           
             
        
             /*
             if (that.data.sorts == "ratio") {
                resRequest.data.data.sort(that.compare('yongjin','jx'))
                }else if(that.data.sorts == "price") {
               resRequest.data.data.sort(that.compare('price','sx'))
             } */  
             that.setData({
               couponList: that.data.couponList.concat(resRequest.data.data.result),
               isLoading: false
             })
           }
           else {
            that.setData({
               isLoading: true,
               loadOver: true
             })
           }
         }
       }
     })
  },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    if (componentId === "cate_method") {
      this.setData({
        selectCategory: selectedId
      })
    }
    if (componentId === "sort_method") {
      this.setData({
        sorts: selectedId
      })
    }
    this.setData({
      [`${componentId}.selectedId`]: selectedId,
      isLoading:true,
      couponList: [],
      pageIndex: 1,
    });
    this.getMoreCouponList()
  },
  onPullDownRefresh: function () {
    this.setData({
      couponList: [],
      isLoading: true,
      pageIndex: 1
    })
    wx.stopPullDownRefresh()
    this.getMoreCouponList()
  },
  onReachBottom: function () {
    this.setData({
      isLoading: true,
      pageIndex: this.data.pageIndex + 1
    })
    this.getMoreCouponList()
  },
  inputTyping: function () {
    wx.navigateTo({
      url: "../../pages/search/search",
    })
  },
  compare: function(property,sort){
    return function(a, b) {
      var value1 = a[property];
      var value2 = b[property];
      if(sort=='jx'){
      return value2 - value1;
      } else { return value1 - value2;}
    }
  },
  tapBanner: function (e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: "/pages/detail/detail?gid=" + e.currentTarget.dataset.id
      })
    }
  },


}));
